package com.bw.zonghe0719.search

import android.os.Build.VERSION_CODES.Q
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.base.mvvm.BaseViewActivity
import com.bw.zonghe0719.R
import com.bw.zonghe0719.databinding.ActivitySearchBinding
import com.bw.zonghe0719.room.AppDBUtils
import com.bw.zonghe0719.room.HistoryEntity
import com.google.android.material.chip.Chip

/**
 * 搜索页面 需求
 * 输入关键词后  回车或点击搜索按钮  实现搜索指定商品  将关键词保存并展示
 */
@Route(path = "/app/search/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewMoel>(){
    override fun initView() {
        queryHis()
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //按回车 会触发
                if(query!=null){
                    AppDBUtils.getData().getHistory().addHistory((HistoryEntity(query,SPUtils.getInstance().getString("phone"))))
                    queryHis()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文字发生变化   实时搜索
                return false
            }

        })
        binding.tvSearch.setOnClickListener {
            AppDBUtils.getData().getHistory().delAllHistory()
            binding.cg.removeAllViews()
        }
    }
    private fun queryHis(){
        //查询数据库
        var listHis =
            AppDBUtils.getData().getHistory().queryHistory(SPUtils.getInstance().getString("phone"))
        //动态添加 chip  创建chip  将chip放到布局中
        if(listHis.size>0){
            //先将之前展示的进行清空
            binding.cg.removeAllViews()
            listHis.reverse()
            listHis.forEach {
                val chip=LayoutInflater.from(this).inflate(R.layout.item_cg,null) as Chip
                //将chip中的text设置内容
                chip.findViewById<TextView>(R.id.name).text=it.keyword
                //将chip添加到cg中
                binding.cg.addView(chip)
                //点击chip控件  search上显示对应内容
                chip.setOnClickListener { it1->
                    binding.sv.setQuery(it.keyword,true)
                }
                //点击chip上的关闭图标 可以将数据删除
                chip.setOnCloseIconClickListener { it2->
                    //先删后台  再删页面
                    //数据库删除
                    AppDBUtils.getData().getHistory().delHistory(HistoryEntity(it.keyword,SPUtils.getInstance().getString("phone")))
                    //页面删除
                    binding.cg.removeView(it2)
                }
            }
        }
    }

    override fun initData() {

    }

    override fun getViewModel(): Class<SearchViewMoel> {
        return SearchViewMoel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_search
    }
}